package com.cskaoyan.service.coupon.admin;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.coupon.CreateCouponBO;
import com.cskaoyan.mapper.CouponMapper;
import com.cskaoyan.mapper.CouponUserMapper;
import com.cskaoyan.tool.CouponTool;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    CouponMapper couponMapper;

    @Autowired
    CouponUserMapper couponUserMapper;


    @Override
    public ListData<Coupon> list(String page, String limit, String sort, String order, String name, String type, Integer status) {
        CouponTool.updateDataSource();
        PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(limit));
        CouponExample couponExample = new CouponExample();
        couponExample.setOrderByClause(sort + " " + order);
        CouponExample.Criteria criteria = couponExample.createCriteria();
        //"%"+name+"%"
        if (name != null) {
            criteria.andNameLike("%" + name + "%");
        }
        if (type != null) {
            criteria.andTypeEqualTo(Short.valueOf(type));
        }
        List<Coupon> coupons = null;
        if (status != null){
            if (status == 0) {
                //正常
                criteria.andDeletedEqualTo(false);
                coupons = couponMapper.selectByExample(couponExample);
                Iterator<Coupon> iterator = coupons.iterator();
                while (iterator.hasNext()){
                    Coupon coupon = iterator.next();
                    boolean b = CouponTool.couponTimeCheck(coupon);
                    if (b){
                        iterator.remove();
                    }
                }
            } else if (status == 1) {
                //已过期
                criteria.andDeletedEqualTo(false);
                coupons = couponMapper.selectByExample(couponExample);
                Iterator<Coupon> iterator = coupons.iterator();
                while (iterator.hasNext()){
                    Coupon coupon = iterator.next();
                    boolean b = CouponTool.couponTimeCheck(coupon);
                    if (! b){
                        iterator.remove();
                    }
                }
            } else if (status == 2) {
                //已下架？已删除？
                criteria.andDeletedEqualTo(true);
                coupons = couponMapper.selectByExample(couponExample);
            }
        }else {
            coupons = couponMapper.selectByExample(couponExample);
        }

        ListData<Coupon> listData = new ListData<>();
        listData.setItems(coupons);

        PageInfo<Coupon> pageInfo = new PageInfo<>(coupons);
        long total = pageInfo.getTotal();
        listData.setTotal((int) total);

        return listData;

    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public boolean delete(int id) {
        int i = couponMapper.deleteByPrimaryKey(id);
        return i == 1;
    }

    /**
     *
     * @param createCouponBO
     * @return
     */
    @Override
    public boolean create(CreateCouponBO createCouponBO) {
        Coupon coupon = getCoupon(createCouponBO);
//SQL语法错误
        int insert = couponMapper.insertSelective(coupon);
        return insert == 1;
    }



    /**
     * 选出刚刚插入的最新的id
     * @return
     */
    @Override
    public int lastInsertId() {
        int i = couponMapper.selectMaxId();
        return i;
    }

    @Override
    public Coupon selectByPrimaryKey(int id) {
        Coupon coupon = couponMapper.selectAllByPrimaryKey(id);
        return coupon;
    }

    @Override
    public boolean update(Coupon coupon) {
        coupon.setUpdateTime(new Date());
        int i = couponMapper.updateByPrimaryKeySelective(coupon);
        return 1 == i;
    }

    @Override
    public ListData<CouponUser> listUser(String page, String limit,String userId,String status, String couponId, String sort, String order) {
        PageHelper.startPage(Integer.valueOf(page),Integer.valueOf(limit));
        CouponUserExample couponUserExample = new CouponUserExample();
        couponUserExample.setOrderByClause(sort + " " + order);
        CouponUserExample.Criteria criteria = couponUserExample.createCriteria();
        if (userId != null){
            criteria.andUserIdEqualTo(Integer.valueOf(userId));
        }
        if (status != null){
            criteria.andStatusEqualTo(Short.valueOf(status));
        }
        if (couponId != null){
            criteria.andCouponIdEqualTo(Integer.valueOf(couponId));
        }
        List<CouponUser> list = couponUserMapper.selectByExample(couponUserExample);
        ListData<CouponUser> listData = new ListData<>();
        listData.setItems(list);

        PageInfo<CouponUser> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        listData.setTotal((int) total);

        return listData;
    }

    /**
     * 根据BO生成Coupon，完全没有必要
     * @param createCouponBO
     * @return
     */
    private Coupon getCoupon(CreateCouponBO createCouponBO) {
        Coupon coupon = new Coupon();
        coupon.setAddTime(new Date());
        coupon.setDays(Short.valueOf(createCouponBO.getDays()));
        coupon.setDesc(createCouponBO.getDesc());
        coupon.setDiscount(createCouponBO.getDiscount());
        coupon.setEndTime(createCouponBO.getEndTime());
        coupon.setGoodsType(createCouponBO.getGoodsType());
//        coupon.setGoodsValue(createCouponBO.getGoodsValue());
        //此处若从前端传入必然是空集合
        if (createCouponBO.getGoodsType() == 0) {
            coupon.setGoodsValue("[]");
        }else {

        }
        if (createCouponBO.getType() == 2){
            UUID uuid = UUID.randomUUID();
            String s = uuid.toString();
            String s1 = s.substring(s.length() - 8,s.length());
            coupon.setCode(s1);
        }
        coupon.setLimit(createCouponBO.getLimit());
        coupon.setMin(createCouponBO.getMin());
        coupon.setName(createCouponBO.getName());
        coupon.setStartTime(createCouponBO.getStartTime());
        coupon.setStatus(createCouponBO.getStatus());
        coupon.setTag(createCouponBO.getTag());
        coupon.setTimeType(createCouponBO.getTimeType());
        coupon.setTotal(Integer.valueOf(createCouponBO.getTotal()));
        coupon.setType(createCouponBO.getType());
        return coupon;
    }

}
